﻿using FileManagerSystem.DbInfos;
using FileManagerSystem.Extend;
using FileManagerSystem.Repository;
using LiteDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace FileManagerSystem.Service
{
    /// <summary>
    /// Service通用实现，增删改查，如果要实现自定义的业务功能，请去对应的Interface中提供事务接口，并在实现类中实现对应事务
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
    {
        /// <summary>
        /// 注入的dbContext
        /// </summary>
        [Autowired]
        public IBaseRepository<TEntity> iBaseRepository { get; set; }

        #region 查询
        /// <summary>
        /// 查询全部 --不分页
        /// </summary>
        /// <returns></returns>
        public Task<List<TEntity>> FindAll(Expression<Func<TEntity, bool>> expression)
        {
            return iBaseRepository.FindAll(expression);
        }

        /// <summary>
        /// 查询指定多条 --分页
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public Task<TEntityPaged<TEntity>> FindAllToPage(Expression<Func<TEntity, bool>> expression, int page, int size)
        {
            return iBaseRepository.FindAllToPage(expression, page, size);
        }

        /// <summary>
        /// 查询指定一条
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public Task<TEntity> FindOne(Expression<Func<TEntity, bool>> expression)
        {
            return iBaseRepository.FindOne(expression);
        }

        /// <summary>
        /// 检查对象是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public Task<bool> FindExist(Expression<Func<TEntity, bool>> expression)
        {
            return iBaseRepository.FindExist(expression);
        }
        #endregion

        #region 新增
        /// <summary>
        /// 单条新增
        /// </summary>
        /// <param name="forecast"></param>
        /// <returns></returns>
        public Task<BsonValue> Insert(TEntity forecast)
        {
            return iBaseRepository.Insert(forecast);
        }

        /// <summary>
        /// 单条新增 通过Guid新增
        /// </summary>
        /// <param name="forecast"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public Task<BsonValue> Insert(TEntity forecast, BsonAutoId type)
        {
            return iBaseRepository.Insert(forecast, type);
        }

        /// <summary>
        /// 多条新增
        /// </summary>
        /// <param name="forecast"></param>
        /// <returns></returns>
        public Task<int> BulkInsert(List<TEntity> forecast)
        {
            return iBaseRepository.BulkInsert(forecast);
        }
        #endregion

        #region 更新
        /// <summary>
        /// 单条更新
        /// </summary>
        /// <param name="forecast"></param>
        /// <returns></returns>
        public Task<bool> Update(TEntity forecast)
        {
            return iBaseRepository.UpdateAsync(forecast);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 单挑删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public Task<bool> Delete(BsonValue id)
        {
            return iBaseRepository.Delete(id);
        }

        /// <summary>
        /// 多条删除
        /// </summary>
        /// <param name="expression">删除条件</param>
        /// <returns></returns>
        public Task<int> DeleteMany(Expression<Func<TEntity, bool>> expression)
        {
            return iBaseRepository.DeleteMany(expression);
        }
        #endregion
    }
}
